package com.kitwee.serialport.decoder.mitsubishi;


import com.kitwee.serialport.decoder.InstructionDecoder;

/**
 * 三菱FX系统浮点数解码器
 * 
 * @author zgh zghlx@163.com
 * @date 2016年11月14日 下午4:18:52
 */
public class FXInstructionToFloatDecoder extends InstructionDecoder {


	@Override
	public  synchronized Object decode(byte[] source) {
		byte[] tempByte = new byte[source.length-4];
		System.arraycopy(source, 1, tempByte, 0, tempByte.length);
		
		char[] sourceChar = new String(tempByte).toCharArray();
		
		char[] temp = new char[sourceChar.length];
		
		int tempIndex = 0;
		for (int i = sourceChar.length-1; i >= 0; i=i-2) {
			if(temp.length>(tempIndex)&&temp.length>1){
				temp[tempIndex++] = sourceChar[i-1];
			}
			temp[tempIndex++] = sourceChar[i];
		}
		
		tempByte = new byte[temp.length];
		if(temp.length >= 8){
			Float ret =  byte2float(new String(temp));
			return ret.intValue();
		}

		return byteArray2int(asciiTransformByte(temp));
	}

}
